import {
  tree,
  allTree,
  checkCode,
  add,
  remove,
  treeNotDealer,
  details,
  edit,
  treeByRoot,
  batchAddDealer,
  findDealer
} from '../services/org';

export default {
  namespace: 'org1',
  state: {
    dataList: [],
    treeData: [],
    treeDataNotDealer: [],
    loading: false,
    details: {},
    detailsLoading: false,
    dealer:{
      list:[],
      pagination:{},
    }
  },
  effects: {
    *initData({ payload, callback }, { call, put }) {
      yield put({
        type: 'save', payload: {
          dataList: [],
          treeData: [],
          treeDataNotDealer: [],
          loading: false,
          details: {},
          detailsLoading: false,
          dealer:{
            list:[],
            pagination:{},
          }
        }
      });
    },
    *findDealer({payload,callback},{call,put}){
      const response=yield call(findDealer,payload);
      yield put({
        type:'save',
        payload:{dealer:response,loading:false}
      });
      if(callback) callback(response);
    },
    // 获取组织机构树
    *fetchTree({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(tree, payload);
      yield put({
        type: 'save',
        payload: { treeData: response.data, loading: false },
      });
      if (callback) callback(response);
    },
    // 获取组织机构树
    *fetchAllTree({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(allTree, payload);
      yield put({
        type: 'save',
        payload: { treeData: response.data, loading: false },
      });
      if (callback) callback(response);
    },
    *fetchTreeNotDealer({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(treeNotDealer, payload);
      yield put({
        type: 'save',
        payload: { loading: false, treeDataNotDealer: [response] },
      });
      if (callback) callback(response);
    },
    *checkCode({ payload, callback }, { call, _ }) {
      const response = yield call(checkCode, payload);
      if (callback) callback(response);
    },
    // 组织添加
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(add, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
    *edit({ payload, callback }, { call, put }) {
      const response = yield call(edit, payload);
      yield put({
        type: 'save',
        payload: { details: response.data },
      });
      if (callback) callback(response);
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
    // 获取组织机构详情
    *details({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { detailsLoading: true },
      });
      const response = yield call(details, payload);
      yield put({
        type: 'save',
        payload: { details: response.data },
      });
      yield put({
        type: 'save',
        payload: { detailsLoading: false },
      });
      if (callback) callback(response);
    },
    *clearDetails({ payload }, { call, put }) {
      yield put({
        type: 'save',
        payload: { details: {}}
      })
    },
    *getByRoot({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(treeByRoot, payload);
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
    *batchAddDealer({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(batchAddDealer, payload);
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
